home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 1.iso / dist / fw_exmh.idb / usr / freeware / lib / exmh-2.5 / post.tcl.z / post.tcl
Text File  |  2002-07-08  |  8KB  |  290 lines

  1. #
  2. # post.tcl  -  news posting client for exmh
  3. # Needs tcl7.5/tk4.1 or above.
  4. # Gareth Owen (g.owen@aston.ac.uk)
  5. # 20-Dec-96
  6.  
  7.  
  8. proc Post_Init {} {
  9.  
  10.     global NNTP
  11.  
  12.     Preferences_Add "NNTP Support" \
  13. "Post a mail message to a Usenet news server via NNTP
  14. (mainly intended to take the grind out of moderating newsgroups),
  15. and retrieve new articles from selected newsgroups." {
  16.     {NNTP(host) nntpHost {news} {News Server}
  17. "The name of your usenet news server. Very often 'news'. "}
  18.     {NNTP(port) nntpPort {119}  {NNTP port}
  19. "Port on which the news server listens for nntp connections. 
  20. 119 unless your site is weird"}
  21.     {NNTP(emailaddr) nntpEmailAddr {}  {My address when posting}
  22. "E-mail address to use when posting to newsgroups.
  23. Typically: First Last <login@domain>"}
  24.      {NNTP(user) nntpUser {} {NNTP Username}
  25. "If your news server requires you to authenticate put your
  26. username here else leave blank"}
  27.     {NNTP(pass) nntpPass {} {NNTP Password}
  28. "If your news server requires you to authenticate put your
  29. password here. Ignored unless Username is defined."}
  30.    {NNTP(moderated) nntpModerated {} {Groups you moderate}
  31. "A list of groups which you moderate separated by whitespace.
  32. If one of these is in the Newsgroups header then an Approved
  33. header will be added to the posting"}
  34.     {NNTP(moderator) nntpModerator {} {Moderator's address}
  35. "Address to put in the Approved: field for moderated groups.
  36. Not needed if the moderated list is empty"}
  37.     {NNTP(groups) nntpNewsGroups {} {Groups to retrieve}
  38. "A list of groups which you wish to be retrieved when the
  39. \"News\" button is clicked on"}
  40.     {NNTP(newsrc) nntpDotnewsrc {~/.newsrc} {.newsrc file}
  41. "Pathname of your .newsrc file (or equivalent).  If it does not
  42. exist, it will be created.  Defaults to ~/.newsrc"}}
  43. }
  44.  
  45. #
  46. #  Parse mail and sort out what we need
  47. #
  48. proc Post {} {
  49.  
  50.     global NNTP msg env
  51.  
  52.     if [string length $msg(path)] {
  53.          set art [open $msg(path) r]
  54.         set text [read $art]
  55.         close $art
  56.     } else {
  57.     Exmh_Status "Post - no message selected"
  58.     Post_Dismiss
  59.     return
  60.     }
  61.  
  62.     # Headers to throw away since they'll be added anew by INN
  63.     # and CNews which get upset when they can't do it
  64.     set header_throwAway \
  65.     {{return-path:} {received:} {path:} {date:} {message-id:} {to:} \
  66.          {lines:} {x-exmh-isig-} {cc:} {from }}
  67.  
  68.     # Headers INN wants to add  itself get X-original- shoved in front
  69.     # if we want to keep them
  70.     # else MUST be in above else INN will reject post
  71.     # (but NNTP/Cnews doesn't complain about following)
  72.     set header_Xify \
  73.     {{nntp-posting-host:}}
  74.  
  75.     #  split header from body
  76.     set sp [string first "\n\n" $text]
  77.     set spmh [string first "\n--------\n" $text]
  78.     if {$sp < 0 || ($spmh > 0 && $spmh < $sp)} {
  79.     set sp $spmh
  80.     set hdr [string range $text 0 [incr sp -1]]
  81.     set NNTP(body) [string range $text [incr sp 11] end]
  82.     } else {
  83.     set hdr [string range $text 0 [incr sp -1]]
  84.     set NNTP(body) [string range $text [incr sp 3] end]
  85.     }
  86.  
  87.     set headerin [split $hdr \n]
  88.     set NNTP(headers) {}
  89.     set flag 0
  90.     set NNTP(post_groups) $NNTP(groups)
  91.     set NNTP(sender) $NNTP(emailaddr)
  92.     set organization 0
  93.  
  94.     # parse headers and dispose of as necessary
  95.     foreach h $headerin {
  96.     if {$flag} {
  97.         set fstch [string index $h 0]
  98.         if {$fstch == { } || $fstch == {    }} {
  99.         continue
  100.         }
  101.     }
  102.     set lh [string tolower $h]
  103.     set flag 0
  104.     foreach t $header_throwAway {
  105.         if {[string first [string tolower $t] $lh]==0} {
  106.         set flag 1
  107.         break
  108.         }
  109.     }
  110.     if {$flag==0} {
  111.         foreach t $header_Xify {
  112.         if {[string first [string tolower $t] $lh]==0} {
  113.             set h "X-original-$h"
  114.             break
  115.         }
  116.         }
  117.     }
  118.     if {$flag} {
  119.         continue
  120.     } elseif {[string first from: $lh]==0} {
  121.         set NNTP(sender) [string trim [string range $h 5 end]]
  122.     } elseif {[string first newsgroups: $lh]==0} {
  123.         set NNTP(post_groups) [string trim [string range $h 11 end]]
  124.     } elseif {[string first subject: $lh]==0} {
  125.         set NNTP(subject) [string trim [string range $h 8 end]]
  126.     } elseif {[string first organization: $lh]==0} {
  127.         set organization 1
  128.     } else {
  129.         lappend NNTP(headers) $h
  130.     }
  131.     }
  132.     if {$organization==0 && [info exists env(ORGANIZATION)]} {
  133.     set org_hdr "Organization: $env(ORGANIZATION)"
  134.     lappend NNTP(headers) $org_hdr
  135.     }
  136.  
  137.     Post_Widget
  138.  
  139. }
  140.  
  141.  
  142. #
  143. # Create the widget
  144. #
  145. proc Post_Widget {} {
  146.  
  147.     global NNTP
  148.  
  149.     if [Exwin_Toplevel .exmh_post "Post Article to NewsGroup" PostNews] {
  150.  
  151.     .exmh_post.but.quit config -command Post_Dismiss
  152.  
  153.     wm protocol .exmh_post WM_DELETE_WINDOW Post_Dismiss
  154.         Widget_AddBut .exmh_post.but post "Post" Post_Article
  155.  
  156.     frame .exmh_post.ng  -borderwidth 2
  157.     label .exmh_post.ng.l -text Newsgroups: -width 12
  158.     entry .exmh_post.ng.e -textvariable NNTP(post_groups) \
  159.         -relief sunken -width 60
  160.     pack .exmh_post.ng.l -side left -fill both
  161.     pack .exmh_post.ng.e -side left -fill both -expand 1
  162.     pack .exmh_post.ng -expand 1 -fill both
  163.     frame .exmh_post.f -borderwidth 2
  164.     label .exmh_post.f.l -text From: -width 12
  165.     entry .exmh_post.f.e -textvariable NNTP(sender)
  166.     pack .exmh_post.f.l -side left -fill both
  167.     pack .exmh_post.f.e -side left -expand 1 -fill both
  168.     pack .exmh_post.f -side top -expand 1 -fill both
  169.     frame .exmh_post.s -borderwidth 2
  170.     label .exmh_post.s.l -text Subject: -width 12
  171.     entry .exmh_post.s.e -textvariable NNTP(subject)
  172.     pack .exmh_post.s.l -side left -fill both
  173.     pack .exmh_post.s.e -side left -fill both -expand 1
  174.     pack .exmh_post.s -side top -expand 1 -fill both
  175.     
  176.     }
  177. }
  178.  
  179.  
  180.  
  181.  
  182. #
  183. # Construct the article to be posted and invoke poster
  184. #
  185. proc Post_Article {} {
  186.  
  187.     global NNTP
  188.  
  189. # Have we got the essentials ?
  190.     if {! [expr \
  191.       "[string length $NNTP(sender)] && [string length $NNTP(subject)] && [string length $NNTP(post_groups)]"\
  192.           ] } {
  193.     Exmh_Status "Newsgroups , Subject  and From fields are NOT optional"
  194.     return
  195.     }
  196.  
  197.     set head [list "From: $NNTP(sender)" "Subject: $NNTP(subject)" \
  198.           "Newsgroups: $NNTP(post_groups)"]
  199.  
  200. # Is there a moderated group in the list of those we're posting to ?
  201.     set postingTo [split $NNTP(post_groups) ,]
  202.     foreach i $NNTP(moderated) {
  203.     if {[lsearch $postingTo $i]>-1} {
  204.         lappend head "Approved: $NNTP(moderator)"
  205.         break
  206.     }
  207.     }
  208.  
  209. # Join them all up
  210.     regsub "\[ \t\n\]*\$" $NNTP(body) {} body
  211.     set article "[join [concat $head $NNTP(headers)] \n]\n\n$body"
  212.  
  213. # And off we go
  214.     Exmh_Status "Post Article : [busy PostIt $article]"
  215.  
  216.     if [winfo exists .exmh_post] {
  217.         Exwin_Dismiss .exmh_post
  218.     }
  219. }
  220.  
  221. #
  222. # NNTP posting client
  223. #
  224. proc PostIt {article} {
  225.     
  226.     global NNTP
  227.     
  228.     if { [catch {socket $NNTP(host) $NNTP(port)} conn] } {
  229.     return $conn
  230.     }
  231.  
  232.     set line [NNTPReply $conn]
  233.     if {[string first 200 $line]} {
  234.     NNTPCommand $conn QUIT
  235.     close $conn
  236.     return $line
  237.     }
  238.  
  239.     NNTPCommand $conn POST
  240.  
  241.     set line [NNTPReply $conn]
  242.     if {[string first 340 $line]} {
  243.     set ok 0
  244.     if ![string first 480 $line] {
  245.         set ok [NNTPAuthenticate $conn]
  246.         if $ok {
  247.         NNTPCommand $conn POST
  248.         set line [NNTPReply $conn]
  249.         if [string first 340 $line] {
  250.             set ok 0
  251.         }
  252.         }
  253.     }    
  254.     if {!$ok} {
  255.         NNTPCommand $conn QUIT
  256.         close $conn
  257.         return $line
  258.     }
  259.     }
  260.  
  261.     puts $conn "$article\n.\n"
  262.     flush $conn
  263.     set line [NNTPReply $conn]
  264.     if {[string first 240 $line]} {
  265.     NNTPCommand $conn QUIT
  266.     close $conn
  267.     return $line
  268.     }
  269.     
  270.     NNTPCommand $conn QUIT
  271.     set line [NNTPReply $conn]
  272.     if {[string first 205 $line]} {
  273.     NNTPCommand $conn QUIT
  274.     close $conn
  275.     return $line
  276.     }
  277.     
  278.     close $conn
  279.     return "Posted Successfully"
  280.  
  281. }
  282.  
  283. proc Post_Dismiss {} {
  284.     if [winfo exists .exmh_post] {
  285.     Exwin_Dismiss .exmh_post
  286.     }
  287. }
  288.  
  289.